package com.self.campussecondhandmarket.controller.user;

import com.self.campussecondhandmarket.content.ImageContent;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Author: Vincent
 * <p> DateTime: 2024/4/12 11:04
 * <p> Description: 图片上传统一接口
 * <p>
 **/
@RestController
@RequestMapping("/api/image")
public class ImagesController {
	
	/**
	 * 商品图片
	 */
	@PostMapping("/goods/upload")
	public String uploadImagWithGoods(MultipartFile file) throws IOException {
		String fileName = file.getOriginalFilename();
		byte[] bytes = file.getBytes();
		try (FileOutputStream fileOutputStream = new FileOutputStream(ImageContent.GOODS_IMAGE_FILE_PATH + fileName)) {
			fileOutputStream.write(bytes);
		}
		return ImageContent.GOODS_IMAGE_INTERNET_PATH + fileName;
	}
	
	/**
	 * 论坛图片
	 */
	@PostMapping("/forum/upload")
	public String uploadImageWithForum(MultipartFile file) throws IOException {
		String fileName = file.getOriginalFilename();
		byte[] bytes = file.getBytes();
		try (FileOutputStream fileOutputStream = new FileOutputStream(ImageContent.FORUM_IMAGE_FILE_PATH + fileName)) {
			fileOutputStream.write(bytes);
		}
		return ImageContent.FORUM_IMAGE_INTERNET_PATH + fileName;
	}
}
